package com.joker.aigc.chat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joker.aigc.annotation.FrequencyControl;
import com.joker.aigc.chat.domain.dto.MessageDto;
import com.joker.aigc.chat.domain.dto.MessageMemberDto;
import com.joker.aigc.chat.domain.dto.MessageQueryCriteria;
import com.joker.aigc.chat.domain.dto.MessageReadDto;
import com.joker.aigc.chat.domain.dto.MessageReadInfoCriteria;
import com.joker.aigc.chat.domain.dto.MessageRecallDto;
import com.joker.aigc.domain.entity.R;
import com.joker.aigc.chat.domain.vo.MessageReadInfoVo;
import com.joker.aigc.chat.domain.vo.MessageReadVo;
import com.joker.aigc.chat.domain.vo.MessageVo;
import com.joker.aigc.chat.service.MessageService;
import com.joker.aigc.utils.RequestHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/api/1.0/message")
@Tag(name = "message", description = "消息管理相关接口")
public class MessageController {


    final MessageService messageService;


    @PostMapping("/msg")
    @Operation(summary = "用户发送消息", description = "用户发送消息")
    @FrequencyControl(time = 5, count = 3, target = FrequencyControl.Target.UID)
    @FrequencyControl(time = 30, count = 5, target = FrequencyControl.Target.UID)
    @FrequencyControl(time = 60, count = 10, target = FrequencyControl.Target.UID)
    public R<MessageVo> sendMsg(@Valid @RequestBody MessageDto request) {
        log.info("sendMsg-->{}", request);
        return R.ok(messageService.sendMsg(request, RequestHolder.get().getUid()));
    }


    /**
     * @param request 互殴
     * @return
     */
    @Operation(summary = "获取某一房间里面的用户信息", description = "获取某一房间里面的用户信息")
    @PostMapping("/public/msg/page")
    public R<Page<MessageVo>> queryMessage(@Valid @RequestBody MessageQueryCriteria request) {
        return R.ok(messageService.queryMessage(request));
    }


    @PutMapping("/msg/recall")
    @Operation(description = "撤回消息")
    @FrequencyControl(time = 20, count = 3, target = FrequencyControl.Target.UID)
    public R<String> recallMsg(@Valid @RequestBody MessageRecallDto request) {
        messageService.recallMsg(request);
        return R.ok("撤回成功");
    }


    @PostMapping("/msg/read/page")
    @Operation(summary = "获取消息的已读未读列表")
    public R<Page<MessageReadVo>> getReadPage(@Valid @RequestBody MessageReadDto messageReadDto) {
        Long uid = RequestHolder.get().getUid();
        return R.ok(messageService.getReadPage(uid, messageReadDto));
    }

    @PutMapping("/msg/read")
    @Operation(summary = "获取消息的已读未读总数")
    public R<List<MessageReadInfoVo>> getReadInfo(@Valid @RequestBody MessageReadInfoCriteria request) {
        Long uid = RequestHolder.get().getUid();
        return R.ok(messageService.getReadInfo(uid, request));
    }


    @PostMapping("/msg/read")
    @Operation(summary = "消息阅读上报")
    public R<String> msgRead(@Valid @RequestBody MessageMemberDto request) {
        Long uid = RequestHolder.get().getUid();
        messageService.msgRead(uid, request);
        return R.ok("阅读成功");
    }


    @GetMapping("/hello")
    public String hello() {
        return "message --> hello";
    }

}
